A Brutalist's Guide to Regex
Example 8: Finding all occurrences of /b/ with Replacing them with /b
Use the preg_match_all() function to find all occurrences of /b/ in the text, then use preg_replace() to wrap them in /b.
For this example, let's assume we have the following text: "Hello, world!
First, we use preg_match_all() to find all occurrences of /b/ in the text:
preg_match_all('/b/', $text, $matches);
This will return an array of all matches, which we can then use with preg_replace() to replace them with /b:
preg_replace('/b/', '/b', $text);
And that's it!
See example 8.1 for the full code for this example.
Next Example 9: /a Tag Hacking